package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public class DataStar extends DataGeometry implements ITag {
	public static String DEFAULT_ID = "_Star";
		DataSize radiusA = new DataSize(20);
		DataSize radiusViewA = new DataSize(20);
		DataSize radiusB = new DataSize(10);
		DataSize radiusViewB = new DataSize(10);
		DataSize sides = new DataSize(TagModifier.SIZE_TYPE_COUNT,5);

		int lastSides;
		int lastRadiusA;
		int lastRadiusB;

		DataPoints _points = new DataPoints();
		DataPoints _pointsView = new DataPoints();

		GeometryPolygon _polygon = null;
		DataTranslation _translation;
public DataStar() {
	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public void buildFromgrfxML(XmlElement xml)
{

	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.RADIUS_A))
		{
			radiusA = new DataSize(TagModifier.SIZE_TYPE_SIZE,10);
	  		radiusA.buildFromgrfxML(element,TagModifier.SIZE_TYPE_SIZE);
		}
		else if(eleName.equals(Token.RADIUS_B))
		{
			radiusB = new DataSize(TagModifier.SIZE_TYPE_SIZE,10);
	  		radiusB.buildFromgrfxML(element,TagModifier.SIZE_TYPE_SIZE);
		}
		else if(eleName.equals(Token.SIDES))
		{
			sides = new DataSize(TagModifier.SIZE_TYPE_COUNT,7);
			sides.buildFromgrfxML(element,TagModifier.SIZE_TYPE_COUNT);
		}
		else if(super.buildFromElement(element)==false && !NameHelper.isSystemName(element.getName()))
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.RPOLYGON,""+element.getStartLineNo()} );
		}
	}
}
public void copyFromgrfxML(XmlElement xml)
{
	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.RADIUS_A))
		{
	  		radiusA.copyFromgrfxML(element,TagModifier.SIZE_TYPE_SIZE);
		}
		else if(eleName.equals(Token.RADIUS_B))
		{
	  		radiusB.copyFromgrfxML(element,TagModifier.SIZE_TYPE_SIZE);
		}
		else if(eleName.equals(Token.SIDES))
		{
			sides.copyFromgrfxML(element,TagModifier.SIZE_TYPE_COUNT);
		}
		else if(super.copyFromElement(element)==false)
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.RPOLYGON,""+element.getStartLineNo()} );
		}
	}
}
public DataGeometry getClone() {

	DataStar newStar = new DataStar();

	super.getClonedElements(newStar);

	newStar.radiusA = radiusA.getClone();
	newStar.radiusB = radiusB.getClone();
	newStar.sides = sides.getClone();

	return newStar;
}
public DataGeometry getClone(String path) {

	DataStar newStar = new DataStar();

	GrfxMLStoreProcessor.setCloneName( path,this,newStar );

	super.getClonedElements(path,newStar);

	newStar.radiusA = radiusA.getClone(path);
	newStar.radiusB = radiusB.getClone(path);
	newStar.sides = sides.getClone(path);

	return newStar;
}
public Geometry getGeometry(GrfxMLEngine engine)
{
	if(_polygon==null && _translation!=null && color!=null)
	{
		_polygon = new GeometryPolygon(
				engine,
				_translation.window,
				bounds,
				_translation.visibleView,
				transparency,
				color,
				filled,
				DataState.TRUE,
				_translation.positionView,
				offsetView,
				_pointsView,
				new DataSize(1));
		if( !NameHelper.isSystemName(getId()) )
		{
			_polygon.setId(getId());
		}
	}

	if(sides.getValue()!=lastSides || radiusA.getValue()!=lastRadiusA || radiusB.getValue()!=lastRadiusB)
	{
		lastSides = sides.getValue();
		lastRadiusA = radiusA.getValue();
		lastRadiusB = radiusB.getValue();

		GeometryProcessor.process(this);
	}

	return(_polygon);
}
public DataPoints getPoints() {
	return _points;
}
		public java.awt.Polygon getPolygon() {
	return _polygon.polygon;
}
public DataSize getSides() {
	return sides;
}
public DataTranslation getTranslation() {
	return _translation;
}
public void register(DataLinker store) {

	super.register(store);

	try{
		store.add(radiusA,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(radiusB,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(sides,this);
	}
	catch(LockException le)
	{}//Dummy catch
}
public void replace(Field oldObj, Field newObj) {
	replace(oldObj, newObj, false);
}
public void replace(Field oldObj, Field newObj,boolean recursive) {

	_polygon=null;

	super.replace(oldObj,newObj);

	if(radiusA==oldObj)
		radiusA=(DataSize)newObj;

	if(radiusB==oldObj)
		radiusB=(DataSize)newObj;

	if(sides==oldObj)
		sides=(DataSize)newObj;
}
public void resetChanged() {
	super.resetChanged();
	radiusA.resetChanged();
	radiusViewA.resetChanged();
	radiusB.resetChanged();
	radiusViewB.resetChanged();
	sides.resetChanged();
	_points.resetChanged();
	_pointsView.resetChanged();
}
protected void setPoints(DataPoints newPoints) {
	_points = newPoints;
}
protected void setSides(DataSize newSides) {
	sides = newSides;
}
protected void setTranslation(DataTranslation newTranslation) {
	_translation = newTranslation;
}
public String togrfxML(String name) {
	String xml = "<" + name;

	if(!NameHelper.isSystemName(getId()))
	{
		xml += " id=\""+getId()+"\"";
	}

	xml += ">";
	xml += super.togrfxML(name);
	xml += radiusA.togrfxML(new DataSize(20),Token.RADIUS_A);
	xml += radiusB.togrfxML(new DataSize(10),Token.RADIUS_B);
	xml += sides.togrfxML(new DataSize(5),Token.SIDES);
	xml += "</" + name + ">\n";
	return xml;
}
public String toString()
{
	if(GrfxMLEngine.DEBUG)
		return(togrfxML( DEFAULT_ID.substring(1) ));

	String xml = "<"+DEFAULT_ID.substring(1)+"/>";

	return xml;
}
public void translate(DataTranslation t) {

	if (_translation == null)
		_translation = t;

	try {
		GrfxMLProcessor.translate(_points,TagModifier.POINT_TYPE_POINT, t, _pointsView);
	} catch (LockException le) {
		le.printStackTrace();
	}
	super.translate(t);
}
}
